package com.yanqu.road.dao.impl.cross.unionwar;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarState;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionWarStateDaoImpl extends TempDataObjectDao<CrossUnionWarState> {


    public Map<Integer, Map<Integer, Map<Integer, List<CrossUnionWarState>>>> getCrossUnionWarState(List<Integer> activityIdList) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, Map<Integer, List<CrossUnionWarState>>>> dataMap = new ConcurrentHashMap<>();
        if(null == activityIdList || activityIdList.size() <= 0){
            return dataMap;
        }
        String getCrossUnionWarStateSql = "select * from t_c_activity_unionwar_state where activityId in(" + StringUtils.listToString(activityIdList, ",") + ") ";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossUnionWarStateSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUnionWarState data = parse(rs);
                if(!dataMap.containsKey(data.getActivityId())){
                    dataMap.put(data.getActivityId(), new ConcurrentHashMap<>());
                }
                if(!dataMap.get(data.getActivityId()).containsKey(data.getStageNo())){
                    dataMap.get(data.getActivityId()).put(data.getStageNo(), new ConcurrentHashMap<>());
                }
                if(!dataMap.get(data.getActivityId()).get(data.getStageNo()).containsKey(data.getRoundNo())){
                    dataMap.get(data.getActivityId()).get(data.getStageNo()).put(data.getRoundNo(), new ArrayList<>());
                }
                dataMap.get(data.getActivityId()).get(data.getStageNo()).get(data.getRoundNo()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String insertCrossUnionWarStateSql = "insert into t_c_activity_unionwar_state(activityId,stageNo,roundNo,state,groupId) values (?,?,?,?,?)";

    @Override
    protected boolean addData(CrossUnionWarState data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStageNo()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoundNo()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getState()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(insertCrossUnionWarStateSql, para) > 0) {
            return true;
        }
        return false;
    }


    public static String updateCrossUnionWarStateSql = "update t_c_activity_unionwar_state set state=? where activityId=? and stageNo=? and roundNo=? and groupId=?";

    @Override
    protected boolean updateData(CrossUnionWarState data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getState()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStageNo()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoundNo()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(updateCrossUnionWarStateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossUnionWarState parseData(ResultSet rs) throws SQLException {
        CrossUnionWarState data = new CrossUnionWarState();
        data.setActivityId(rs.getInt("activityId"));
        data.setStageNo(rs.getInt("stageNo"));
        data.setRoundNo(rs.getInt("roundNo"));
        data.setState(rs.getInt("state"));
        data.setGroupId(rs.getInt("groupId"));
        return data;
    }
}
